package helpers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class DateHelper {
	public static List<Long> calDistanceTimeToNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Date now = new Date();
		long seconds = (now.getTime() - date.getTime()) / 1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day * 86400) / 3600);
		long minute = (int) ((seconds - day * 86400 - hour * 3600) / 60);
		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}

	public static List<Long> coundownFromNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Calendar cal = Calendar.getInstance();
		Date now = new Date();
		cal.setTime(date);
		cal.add(Calendar.DATE, +3);

		long seconds = (cal.getTime().getTime() - now.getTime()) / 1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day * 86400) / 3600);
		long minute = (int) ((seconds - day * 86400 - hour * 3600) / 60);

		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}

	/**
	 * 
	 * Example: System.out.println(DateHelper.isValidDate("2004-02-29"));
	 * 
	 */
	public static boolean isValidDate(String inDate) {

		if (inDate == null)
			return false;

		// set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		if (inDate.trim().length() != dateFormat.toPattern().length())
			return false;

		dateFormat.setLenient(false);

		try {
			// parse the inDate parameter
			dateFormat.parse(inDate.trim());
		} catch (ParseException pe) {
			return false;
		}
		return true;
	}

	/**
	 * @param inDate
	 *            ex: 2012-02-29
	 * @param format
	 *            ex: yyyy-MM-dd
	 * @return
	 */
	public static Date StringToDate(String inDate, String format) {
		Date date = new Date();
		if (inDate == null)
			return date;

		// set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);

		if (inDate.trim().length() != dateFormat.toPattern().length())
			return date;

		dateFormat.setLenient(false);

		try {
			// parse the inDate parameter
			date = dateFormat.parse(inDate.trim());
		} catch (ParseException pe) {
			return date;
		}
		return date;
	}

	public static String dateToString(Date date, String format) {
		try {
			String result = "";
			// set the format to use as a constructor argument
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+07"));
			result = dateFormat.format(date);
			return result;
		} catch (Exception e) {
			return "";
		}

	}

	public static Date removeTimeFromDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
}
